package servlet.peopel;

import bean.Area;
import bean.People;
import bean.Salary;
import dao.PeopleDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by young on 2017/6/24.
 */
@WebServlet(name = "AddPeople", urlPatterns = "/addPeople")
public class AddPeople extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setHeader("content-type", "text/html;charset=UTF-8");
        People people = new People();
        people.setName(request.getParameter("peopleName"));
        people.setAge(request.getParameter("peopleAge"));
        people.setSex(request.getParameter("peopleSex"));
        Area area = new Area();
        area.setId(Integer.parseInt(request.getParameter("areaId")));
        people.setArea(area);
        PeopleDao peopleDao = new PeopleDao();
        Salary salary = new Salary();
        salary.setId(Integer.parseInt(request.getParameter("salaryId")));
        people.setSalary(salary);
        int row = peopleDao.addPeople(people);
        if (row != 0) {
            response.sendRedirect("/peopleManger.jsp");
        } else {
            response.sendRedirect("/errorPage.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
